package com.newlink.building.login.utils

import android.app.Activity
import android.graphics.Rect
import android.view.View
import android.view.ViewTreeObserver
import android.view.inputmethod.InputMethodManager
import android.widget.ScrollView
import androidx.core.widget.NestedScrollView

class Module_Login_KeyboardUtils {
    
    companion object {
        
        fun setupKeyboardHandling(activity: Activity, scrollView: View?, focusedView: View? = null) {
            val rootView = activity.window.decorView.rootView
            var isKeyboardShowing = false
            
            val listener = ViewTreeObserver.OnGlobalLayoutListener {
                val rect = Rect()
                rootView.getWindowVisibleDisplayFrame(rect)
                val screenHeight = rootView.height
                val keypadHeight = screenHeight - rect.bottom
                
                val isKeyboardOpen = keypadHeight > screenHeight * 0.15
                
                if (isKeyboardOpen && !isKeyboardShowing) {
                    // Keyboard is now open
                    isKeyboardShowing = true
                    onKeyboardOpen(activity, scrollView, focusedView)
                } else if (!isKeyboardOpen && isKeyboardShowing) {
                    // Keyboard is now closed
                    isKeyboardShowing = false
                    onKeyboardClosed(activity, scrollView)
                }
            }
            
            rootView.viewTreeObserver.addOnGlobalLayoutListener(listener)
        }
        
        private fun onKeyboardOpen(activity: Activity, scrollView: View?, focusedView: View?) {
            // Scroll to the focused view when keyboard opens
            focusedView?.let { view ->
                scrollView?.postDelayed({
                    when (scrollView) {
                        is ScrollView -> {
                            val scrollTo = getRelativeTop(view, scrollView)
                            scrollView.smoothScrollTo(0, scrollTo - 100)
                        }
                        is NestedScrollView -> {
                            val scrollTo = getRelativeTop(view, scrollView)
                            scrollView.smoothScrollTo(0, scrollTo - 100)
                        }
                    }
                }, 200)
            }
        }
        
        private fun onKeyboardClosed(activity: Activity, scrollView: View?) {
            // Optional: Reset scroll position when keyboard closes
            // scrollView?.smoothScrollTo(0, 0)
        }
        
        private fun getRelativeTop(child: View, parent: View): Int {
            var offsetTop = 0
            var currentView: View? = child
            
            while (currentView != null && currentView != parent) {
                offsetTop += currentView.top
                currentView = currentView.parent as? View
            }
            
            return offsetTop
        }
        
        fun hideKeyboard(activity: Activity) {
            val imm = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
            val view = activity.currentFocus ?: View(activity)
            imm.hideSoftInputFromWindow(view.windowToken, 0)
        }
        
        fun showKeyboard(activity: Activity, view: View) {
            view.requestFocus()
            val imm = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
        }
    }
}